package com.android.scanner.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Handler;
import android.os.Message;

public class HttpUtil {

	public static void httppost(final String url,
			final List<NameValuePair> params, final Handler handler,
			final int msgid) {

		new Thread() {
			public void run() {
				/* ����HTTP Post���� */
				HttpPost httpRequest = new HttpPost(url);
				String strResult = null;
				String param = "";
				try {
					for (int i = 0; i < params.size(); i++) {
						if (i == 0) {
							param = param + params.get(i);
						} else {
							param = param + "&" + params.get(i);
						}
					}
					// ����HTTP request
					// httpRequest.setEntity(new UrlEncodedFormEntity(params,
					// HTTP.UTF_8));
					httpRequest.setEntity(new StringEntity(param, HTTP.UTF_8));
					// httpRequest.setEntity(new StringEntity(param));

					// ȡ��HTTP response
					HttpResponse httpResponse = new DefaultHttpClient()
							.execute(httpRequest);
					// ��״̬��Ϊ200 ok
					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// ȡ����Ӧ�ִ�
						strResult = EntityUtils.toString(
								httpResponse.getEntity(), HTTP.UTF_8);

					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (handler != null) {
						Message msg = new Message();
						msg.what = msgid;
						msg.obj = strResult;
						handler.sendMessage(msg);
					}
				}
			};
		}.start();

	}

	
	public static void httpget(final String url, final Handler handler,
			final int msgid) {
		new Thread() {
			public void run() {
				HttpGet httpRequest = new HttpGet(url);
				String strResult = null;
				try {
					HttpResponse httpResponse = new DefaultHttpClient()
							.execute(httpRequest);

					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// ȡ����Ӧ�ִ�
						strResult = EntityUtils.toString(
								httpResponse.getEntity(), HTTP.UTF_8);

					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (handler != null) {
						Message msg = new Message();
						msg.what = msgid;
						msg.obj = strResult;
						handler.sendMessage(msg);
					}
				}
			};
		}.start();

	}

	public static void httpconn(final String url, final Handler handler,
			final int msgid) {
		new Thread() {
			public void run() {

				HttpURLConnection conn = null;
				URL connUrl;
				InputStream is = null;
				String strResult = null;
				try {
					connUrl = new URL(url);
					conn = (HttpURLConnection) connUrl.openConnection();
					conn.setConnectTimeout(20 * 1000);
					conn.setRequestMethod("GET");
					conn.connect();

					if (conn.getResponseCode() == 200) {
						is = conn.getInputStream();
						strResult = readData(is, "UTF-8");
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {

					try {
						if (handler != null) {
							Message msg = new Message();
							msg.what = msgid;
							msg.obj = strResult;
							handler.sendMessage(msg);
						}
						if (conn != null)
							conn.disconnect();
						if (is != null)
							is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
		}.start();

	}

	public static void httpconn2(final String url, final Handler handler,
			final int msgid) {
		HttpURLConnection conn = null;
		URL connUrl;
		InputStream is = null;
		String strResult = null;
		try {
			connUrl = new URL(url);
			conn = (HttpURLConnection) connUrl.openConnection();
			conn.setConnectTimeout(20 * 1000);
			conn.setRequestMethod("GET");
			conn.connect();

			if (conn.getResponseCode() == 200) {
				is = conn.getInputStream();
				strResult = readData(is, "UTF-8");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				if (handler != null) {
					Message msg = new Message();
					msg.what = msgid;
					msg.obj = strResult;
					handler.sendMessage(msg);
				}
				if (conn != null)
					conn.disconnect();
				if (is != null)
					is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public static String readData(InputStream inSream, String charsetName)
			throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while ((len = inSream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inSream.close();
		return new String(data, charsetName);
	}
}
